<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>DOM元素节点查找</title>
    <style>
      .c1 {
        color: red;
      }
      .c2 {
        color: green;
      }
    </style>
  </head>
  <body>
    <ul id="ul1">
      <li class="c1">ul1-li1</li>
      <li class="c2">ul1-li2</li>
      <li class="c1">
        ul1-li3
        <ul id="ul2">
          <li class="c1">ul2-li1</li>
          <li class="c2">ul2-li2</li>
          <li class="c1">ul2-li3</li>
        </ul>
      </li>
    </ul>
    <script>
      // 根据id查找
      const ul2 = document.getElementById("ul2");
      //   ul2 = document.querySelector("#ul2");
      console.log(ul2);
      // 根据css选择器，查找符合选择器的所有元素节点
      const elems = ul2.querySelectorAll("li.c1");
      for (let elem of elems) {
        console.log(elem);
      }
      // 根据css选择器，查找符合选择器的第一个元素节点
      const elem = ul2.querySelector("li.c1");
      console.log(elem);
      // 查找最近的父元素节点
      console.log(elem.closest("ul"));
    </script>
  </body>
</html>
